package register.ucm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.Controls.*;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.util.BufferToImage;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import com.bruceeckel.swing.Console;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.control.*;
import javax.media.format.*;
import register.view.RegisterPanel;
import register.model.ButtonModel;
import javax.swing.JLabel;
import register.model.PhotoPanelValueModel;
import register.model.PhotoPanelButtonModel;

/**
 *
 */
/**
 * @author Vitamin
 *
 */
public class PhotoCamera extends JApplet {

    private static final long serialVersionUID = 1L;
    private ButtonModel button;
    private JLabel label;
    private static String id;
    public static Player player = null;
    private CaptureDeviceInfo di = null;
    private MediaLocator ml = null;
    private JButton capture = new JButton("Start Camera");
    private JButton grab = new JButton("|                                Take Photo                              |");
    private JButton ruler = new JButton("|                                  |");
    private JLabel labelPath =new JLabel();
    String str = "vfw:Microsoft WDM Image Capture (Win32):0";
    private PhotoPruner cutter=new PhotoPruner();
    private ImageIcon photo;
    private String path;
    private PhotoPanelValueModel subValue;
    private PhotoPanelButtonModel subButton;
    private static PhotoCamera local;
    private RegisterPanel main;
    private GetPathFromPhoto pathTemp;
    public PhotoCamera() {
        button=RegisterPanel.getInstance().getButtonModel();
        label=button.getPhoto();
        main=RegisterPanel.getInstance();
        subButton=new PhotoPanelButtonModel();
        subValue=new PhotoPanelValueModel();
    }

    ActionListener cl = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            captureImg();
        }
    };
    ActionListener gp = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            takePhoto();
        }
    };

    public static PhotoCamera getInstance()
    {
        if(local==null)
        {
            local=new PhotoCamera();
        }

        return local;
    }


    public PhotoPanelButtonModel getPhotoPanelButtonModel()
    {
        subButton.setPath(labelPath);
        return subButton;
    }

    public PhotoPanelValueModel getPhotoPanelValueModel()
    {
        subValue.setPath(path);
        return subValue;
    }

    public void takePhoto() {

        try {
            FrameGrabbingControl frameGrabber = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
            Buffer buf = frameGrabber.grabFrame();

            Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
            BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = buffImg.createGraphics();
            g.drawImage(img, null, null);

            ImageIO.write(buffImg, "JPG", new File("D:\\TP_ORIGINAL\\" + id + ".jpg"));
            player.close();
            player.deallocate();
            cutter.startPrune(id);

            path="D:\\TP_PRUNED\\"+ id +".jpg";
            photo=new ImageIcon(path);
            label.setIcon(photo);
            labelPath.setText(path);
            pathTemp=new GetPathFromPhoto(this);

        } catch (Exception e) {
            System.out.println("Failed to take photo!");
        }
    }

    public void captureImg() {

        di = CaptureDeviceManager.getDevice(str);
        ml = di.getLocator();
        try {
            player = Manager.createRealizedPlayer(ml);
            player.start();
            Component comp;
            if ((comp = player.getVisualComponent()) != null) {
                getContentPane().remove(capture);
                getContentPane().add(ruler, BorderLayout.NORTH);
                getContentPane().add(comp, BorderLayout.CENTER);
                getContentPane().add(grab, BorderLayout.SOUTH);
                getContentPane().doLayout();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    @Override
    public void init() {

        getContentPane().add(capture, BorderLayout.SOUTH);
        getContentPane().add(labelPath);

        capture.addActionListener(cl);
        grab.addActionListener(gp);
    }

    public void run(String temp) {
        id=temp;
        PhotoCamera jc = new PhotoCamera();
        Console.run(jc, 350, 350);
    }

}